package apiUtils.apis.account;

import android.content.Context;
import android.text.TextUtils;

import com.example.jhlibrarys.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import apiUtils.APISHelper;
import apiUtils.CallBack;
import apiUtils.params.DESParams;
import apiUtils.params.Params;
import appUtils.AppUtil;
import appUtils.PayTokenException;
import constant.ApiConstants;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

import static apiUtils.BaseAPIS.NETWORK_UNKNOWN;
import static constant.ApiConstants.TASK_SUCCESS;

/**
 * Created by Administrator on 2017/5/5.
 */

public abstract class BaseAccountAPI extends APISHelper {
    String mSubType;
    //account
    String mUserAccount;
    //password
    String mUserPwd;
    Params mApiParams = new DESParams();
    private Context mContext;

    BaseAccountAPI(Context context) {
        mContext = context;
    }

    void initAccountParams() throws JSONException {
        mApiParams.put(mContext.getString(R.string.paramSystemName), ApiConstants.SYSTEM_NAME);
        mApiParams.put(mContext.getString(R.string.paramSubType), mSubType);
    }

    protected void get(String tag, String url, final Listener listener) {
        try {
            get(tag, url, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response);
                        String decodeMsg = desDecode(object);
                        JSONObject jsonObject = new JSONObject(decodeMsg);
                        if (jsonObject.getString(mContext.getString(R.string.resultState)).equals(TASK_SUCCESS)) {
                            if (!TextUtils.isEmpty(decodeMsg)) {
                                success(jsonObject, listener);
                            } else {
                                failure(-1, mContext.getString(R.string.information_error), listener);
                            }
                        } else {
                            String msg = object.getString(mContext.getString(R.string.resultMessage));
                            failure(-1, msg, listener);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        failure(-1, e.getMessage(), listener);
                        PayTokenException.getInstance().saveExceptionFile("BaseAccountAPI get json error:"
                                + e.getMessage());
                    }
                }

                @Override
                public void onError(int code) {
                    if (code == NETWORK_UNKNOWN) {
                        failure(code, AppUtil.getInstance().getContext().getString(R.string.check_network), listener);
                    } else {
                        failure(code, "", listener);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            failure(-1, e.getMessage(), listener);
            PayTokenException.getInstance().saveExceptionFile("BaseAccountAPI get json error:"
                    + e.getMessage());
        }
    }

    protected void post(String tag, String url, final Listener listener) {
        try {
            post(tag, url, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response);
                        String decodeMsg = desDecode(object);
                        JSONObject jsonObject = new JSONObject(decodeMsg);
                        if (jsonObject.getString(mContext.getString(R.string.resultState)).equals(TASK_SUCCESS)) {
                            if (!TextUtils.isEmpty(decodeMsg)) {
                                success(jsonObject, listener);
                            } else {
                                failure(-1, mContext.getString(R.string.information_error), listener);
                            }
                        } else {
                            String msg = jsonObject.getString(mContext.getString(R.string.resultMessage));
                            failure(-1, msg, listener);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        failure(-1, e.getMessage(), listener);
                        PayTokenException.getInstance().saveExceptionFile("BaseAccountAPI post json error:"
                                + e.getMessage());
                    }
                }

                @Override
                public void onError(int code) {
                    if (code == NETWORK_UNKNOWN) {
                        failure(code, AppUtil.getInstance().getContext().getString(R.string.check_network), listener);
                    } else {
                        failure(code, "", listener);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            failure(-1, e.getMessage(), listener);
            PayTokenException.getInstance().saveExceptionFile("BaseAccountAPI post json error:"
                    + e.getMessage());
        }
    }

    private void success(final JSONObject object, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (null != listener) {
                    listener.onSuccess(object);
                }
            }
        });
    }

    private void failure(final int code, final String msg, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (listener != null) {
                    listener.onFailure(code, msg);
                }
            }
        });
    }

    public interface Listener {
        void onSuccess(JSONObject object);

        void onFailure(int code, String message);
    }
}
